﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Bookkeeping.Core.Entities;
using NHibernate;
using Bookkeeping.Persistence;
using Bookkeeping.Core.Services;

namespace Bookkeeping.Tests.Utilities
{
    [TestFixture]
    public class TestDataCreator
    {
        List<Post> _posts;
        List<Tag> _tags;

        [SetUp]
        public void SetUp()
        {
            DateTime thisMonth = DateTime.Today;
            DateTime lastMonth = DateTimeHelper.GetPreviousMonth(thisMonth);

            _tags = new List<Tag>()
            {
                new Tag("Salary"),
                new Tag("Joe"),
                new Tag("Mary"),
                new Tag("Phone"),
                new Tag("Pension")
            };

            _posts = new List<Post>()
            {
                // This month
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = -100,
                    Type = PostType.Expense,
                    Comment = "Joes phone bill",
                    Name = "Phone",
                },
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = -500,
                    Type = PostType.Expense,
                    Comment = "Joes buss pass",
                    Name = "Buss pass",
                },
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = -250,
                    Type = PostType.Expense,
                    Comment = "Bought a new shirt",
                    Name = "Purchase",
                },
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = 20000,
                    Type = PostType.Income,
                    Comment = "Mary's salary",
                    Name = "Salary",
                },
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = 15000,
                    Type = PostType.Income,
                    Comment = "Joe's salary",
                    Name = "Salary",
                },
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = 200,
                    Type = PostType.Saving,
                    Comment = "Mary's pension saving",
                    Name = "Pension",
                },
                new Post()
                {
                    DateTime = thisMonth,
                    Amount = 100,
                    Type = PostType.Saving,
                    Comment = "Joe's pension saving",
                    Name = "Pension",
                },

                // Last month
                new Post()
                {
                    DateTime = lastMonth,
                    Amount = 16000,
                    Type = PostType.Income,
                    Comment = "Joe's salary",
                    Name = "Salary",
                },
                new Post()
                {
                    DateTime = lastMonth,
                    Amount = -170,
                    Type = PostType.Expense,
                    Comment = "Mary's phone bill",
                    Name = "Phone",
                },
                new Post()
                {
                    DateTime = lastMonth,
                    Amount = 100,
                    Type = PostType.Saving,
                    Comment = "Joe's pension saving",
                    Name = "Pension",
                }
            };

            _posts[0].AddTag(_tags[1]);
            _posts[0].AddTag(_tags[3]);
            _posts[3].AddTag(_tags[0]);
            _posts[3].AddTag(_tags[2]);
            _posts[5].AddTag(_tags[4]);
        }

        [Test]
        public void CreateTestData()
        {
            SavePostsAndTags(_posts, _tags);
        }

        public static void SavePostsAndTags(IEnumerable<Post> posts, IEnumerable<Tag> tags)
        {
            using (ISession session = SessionFactorySingleton.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                foreach (var t in tags)
                    session.Save(t);
                foreach (var p in posts)
                    session.Save(p);

                transaction.Commit();
            }
        }

        public static void SavePosts(IEnumerable<Post> posts)
        {
            var tags = new List<Tag>();

            SavePostsAndTags(posts, tags);
        }
    }
}
